package cn.edu.nuc.ssm.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.nuc.ssm.controller.page.PageControl;
import cn.edu.nuc.ssm.controller.page.Pager;
import cn.edu.nuc.ssm.controller.page.Pc;
import cn.edu.nuc.ssm.dao.SysfunctionMapper;
import cn.edu.nuc.ssm.domain.Sysfunction;
import cn.edu.nuc.ssm.dto.SelectSysfunDto;
import cn.edu.nuc.ssm.service.interfaces.FunService;

@Service
public class SysFunctionImpl implements FunService{
    @Autowired
	private SysfunctionMapper sysfunmapper;
    /**
     * 添加系统功能
     */
	@Override
	public int insertFun(Sysfunction sysfun) {
		int result = sysfunmapper.insert(sysfun);
		if(result == 1){
			System.out.println("添加系统功能成功！");
		}else{
			System.out.println("添加系统功能失败！");
		}
		return result;
	}
	/**
	 * 分页显示功能列表
	 */
	@Override
	public Pager<Sysfunction> funlist(String pname,int current) {
		
		PageControl pc = new PageControl();
		Integer currindex = 1;
		if(current != 0){
			currindex = current;
		}
		pc.setCurrentindex(currindex);
		
		//查询总记录数。(如果带有名字，则查询改名字的总记录数。(模糊查询)
		int count = sysfunmapper.count("%"+pname+"%");
		pc.setRscount(count);
		//初始化分页参数。
		pc = Pc.dealpage(pc);
		//查询具体内容
		SelectSysfunDto dto = new SelectSysfunDto();
		dto.setEnd(pc.getCurrentindex()*pc.getPagesize());
		dto.setFname("%"+pname+"%");
		dto.setStart((pc.getCurrentindex()-1)*pc.getPagesize());
		List<Sysfunction> sysfun = sysfunmapper.selectSysFun(dto);
		System.out.println("service层集合大小" + sysfun.size());
		Pager<Sysfunction> pager = new Pager<Sysfunction>();
		pager.setList(sysfun);
		pager.setPagectrl(pc);
		return pager;
	}
	/**
	 * 修改前首先查询功能信息
	 */
	@Override
	public Sysfunction selectByFid(int fid) {
		Sysfunction result = sysfunmapper.selectByPrimaryKey(fid);
		return result;
	}
	/**
	 * 通过主键查询到功能，反馈到页面，更新功能。
	 */
	@Override
	public int updateByPrimaryKey(Sysfunction record) {
		int result = sysfunmapper.updateByPrimaryKey(record);
		return result;
	}
	/**
	 * 通过主键删除功能。
	 */
	@Override
	public int deleteByPrimaryKey(int fid) {
		int result = sysfunmapper.deleteByPrimaryKey(fid);
		return result;
	}

}
